<template>
  <n-modal v-model:show="isVisible" title="编辑物流商信息" preset="dialog" @close="onClose" style="height: 400px;width: 700px">
    <n-form @submit.prevent="handleSubmit" label-width="120px" class="form-container">
      <div class="form-row" style="margin-top: 20px">
        <n-form-item label="物流商名称：" path="name">
          <n-input v-model:value="form.name" placeholder="请输入物流商名称"/>
        </n-form-item>
      </div>
      <div class="form-row">
        <n-form-item label="联系人：" path="contactName" style="width: 250px">
          <n-input v-model:value="form.contactName" placeholder="请输入联系人"/>
        </n-form-item>

        <n-form-item label="联系电话：" path="contactPhone"  style="margin-left: 20px;width: 250px">
          <n-input v-model:value="form.contactPhone" placeholder="请输入联系电话"/>
        </n-form-item>
      </div>

      <div class="form-row">
        <n-form-item label="支持配送区域：" path="supportAddress" style="width: 250px">
          <n-input v-model:value="form.supportAddress" placeholder="请输入支持配送区域"/>
        </n-form-item>
        <n-form-item label="支持配送方式：" path="supportChannel" style="margin-left: 20px;width: 250px">
          <n-input v-model:value="form.supportChannel" placeholder="请输入支持配送方式"/>
        </n-form-item>
      </div>

      <!-- 提交按钮 -->
      <div class="submit-row">
        <n-form-item>
          <n-button type="primary" native-type="submit" @click="handleSubmit">提交</n-button>
        </n-form-item>
      </div>
    </n-form>
  </n-modal>
</template>

<script setup>
import {ref, watch} from 'vue';
import {useUserStore} from "@/store/index.js";
import api from '@/api/admin.js'
import {List} from "echarts";
const userStore = useUserStore()
const id = userStore.getId();
const option = ref([
])
function handleChange(value) {
}
const emit = defineEmits(['updatePage'])
const props = defineProps({
  show1: {
    type: Boolean,
    required: true,
  },
  onClose: {
    type: Function,
    required: true,
  },
  option: {
    type: List,
    require: true
  },
  attribute: {
    type: Object,
    required: true,
  }
});

const isVisible = ref(props.show1);
watch(() => props.show1, (newVal) => {
  isVisible.value = newVal;
  if (newVal) {
    // 当对话框显示时，将传递过来的属性值填充到表单中
    form.value = { ...props.attribute };
  }
});

const form = ref({
  name: '',
  contactName: '',
  contactPhone:'',
  supportAddress:'',
  supportChannel:'',
});

const handleSubmit = async () => {
  try {
    const res = await api.updateLogisticsProvider(form.value);  // 调用更新接口
    if (res.code === 200) {
      emit("updatePage");
    } else {
      $message.error(res.message);
    }
  } catch (err) {
    $message.error("更新失败！");
  } finally {
    clear();
    props.onClose();
  }
};
const clear = () => {
  form.value.name = ""
  form.value.contactName = ""
  form.value.contactPhone=""
  form.value.supportAddress=""
  form.value.supportChannel=""
}
</script>

<style scoped>
.form-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  position: relative;
}

.form-row {
  display: flex;
  align-items: center;
  width: 100%;
}

.form-row > n-form-item {
  flex: 1;
}

.submit-row {
  justify-content: center;
  display: flex;
  width: 100%;
}
</style>
